{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Subtract the Product and Sum of Digits of an Integer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: subtractProductAndSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #整数的各位积和之差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>n</code>，请你帮忙计算并返回该整数「各位数字之积」与「各位数字之和」的差。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 234\n",
    "<strong>输出：</strong>15 \n",
    "<strong>解释：</strong>\n",
    "各位数之积 = 2 * 3 * 4 = 24 \n",
    "各位数之和 = 2 + 3 + 4 = 9 \n",
    "结果 = 24 - 9 = 15\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 4421\n",
    "<strong>输出：</strong>21\n",
    "<strong>解释： \n",
    "</strong>各位数之积 = 4 * 4 * 2 * 1 = 32 \n",
    "各位数之和 = 4 + 4 + 2 + 1 = 11 \n",
    "结果 = 32 - 11 = 21\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10^5</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [subtract-the-product-and-sum-of-digits-of-an-integer](https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [subtract-the-product-and-sum-of-digits-of-an-integer](https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['234', '4421']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a=str(n)\n",
    "        s=1\n",
    "        b=0\n",
    "        for i in a:\n",
    "            s*=int(i)\n",
    "            b+=int(i)\n",
    "        return s-b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ans,ans_1 = 1,0\n",
    "        while n :\n",
    "            n,v= divmod(n,10)\n",
    "            ans *= v\n",
    "            ans_1 += v\n",
    "        return ans - ans_1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        x,y=1,0\n",
    "        while n:\n",
    "            n,v=divmod(n,10)\n",
    "            x*=v\n",
    "            y+=v\n",
    "        return x-y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ls = []\n",
    "        while n != 0:\n",
    "            ls.append(n % 10)\n",
    "            n = n // 10\n",
    "        length = len(ls)\n",
    "        sum = 0\n",
    "        times = 1\n",
    "        for i in range(length):\n",
    "            sum += ls[i]\n",
    "            times *= ls[i]\n",
    "        res = times - sum\n",
    "        return res \n",
    "'''\n",
    "'''\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m, s = 1, 0\n",
    "        while n != 0:\n",
    "            x, n = n % 10, n // 10\n",
    "            m *= x\n",
    "            s += x\n",
    "        return m - s\n",
    "'''\n",
    "'''\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        lst = list(map(int, str(n)))\n",
    "        return prod(lst) - sum(lst)\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a = 0\n",
    "        b = 1\n",
    "        for i in str(n):\n",
    "            a += int(i)\n",
    "            b *= int(i)\n",
    "        return b-a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        num = []\n",
    "        while n != 0:\n",
    "            num.append(n % 10)\n",
    "            n = n // 10\n",
    "        m, s = 1, 0\n",
    "        for i in num:\n",
    "            m *= i\n",
    "            s += i\n",
    "        return m - s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        return reduce(mul, map(int, str(n))) - reduce(add, map(int, str(n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        num_list = []\n",
    "        while n // 10 != 0:\n",
    "            num_list.append(n % 10)\n",
    "            n = n // 10\n",
    "        num_list.append(n)\n",
    "        num_list.reverse()\n",
    "\n",
    "        product = 1\n",
    "        sum = 0\n",
    "        for num in num_list:\n",
    "            product = product * num\n",
    "            sum = sum + num\n",
    "\n",
    "        substractProductSum = product - sum\n",
    "        return substractProductSum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        _product = 1\n",
    "        _sum = 0\n",
    "        for i in str(n):\n",
    "            _product *= int(i)\n",
    "            _sum += int(i)\n",
    "        return _product - _sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a = 1\n",
    "        b = 0\n",
    "\n",
    "        while n>0:\n",
    "            a *= n%10\n",
    "            b += n%10\n",
    "            n//=10\n",
    "        \n",
    "        return a - b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m, s = 1, 0\n",
    "        while n != 0:\n",
    "            x, n = n % 10, n // 10\n",
    "            m *= x\n",
    "            s += x\n",
    "        return m - s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        prod = 1\n",
    "        sum = 0\n",
    "        for c in s:\n",
    "            digit = int(c)\n",
    "            prod *= digit\n",
    "            sum += digit\n",
    "        return prod - sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a=str(n)\n",
    "        l=len(a)\n",
    "        m=s=int(a[0])\n",
    "        i=1\n",
    "        while i < l:\n",
    "            m*=int(a[i])\n",
    "            s+=int(a[i])\n",
    "            i+=1\n",
    "        return m-s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum = 0\n",
    "        acc = 1\n",
    "        while n != 0:\n",
    "            sum += n % 10\n",
    "            acc *= n % 10\n",
    "            n = n // 10\n",
    "        return acc - sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        plus = 0\n",
    "        times = 1\n",
    "        if n != 0:\n",
    "            while n > 0:\n",
    "                plus += n % 10\n",
    "                times *= n % 10\n",
    "                n = int(n / 10)\n",
    "        else:\n",
    "            return 0\n",
    "        return times - plus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        L_temp = list()\n",
    "        qiuji = 1\n",
    "        qiuhe = 0\n",
    "\n",
    "        for i in str(n):\n",
    "            L_temp = L_temp + [i]\n",
    "\n",
    "        for i in L_temp:\n",
    "            qiuji = qiuji * int(i)\n",
    "            qiuhe = qiuhe + int(i)\n",
    "\n",
    "        result = qiuji - qiuhe\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum_1 = 0\n",
    "        sum_2 = 1\n",
    "        while n > 0:\n",
    "            sum_1 = sum_1 + (n % 10)\n",
    "            sum_2 = sum_2 * (n % 10)\n",
    "            n = n // 10\n",
    "            \n",
    "        return sum_2 - sum_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        p,sum = 1,0\n",
    "        for i in s:\n",
    "            p = p*int(i)\n",
    "            sum += int(i)\n",
    "        return p-sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s=str(n)\n",
    "        a=1\n",
    "        b=0\n",
    "        for i in s:\n",
    "            a*=int(i)\n",
    "            b+=int(i)\n",
    "        ans=a-b\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        mul = 1\n",
    "        sum = 0\n",
    "        while n:\n",
    "            sum_temp = n\n",
    "            mul_temp = n\n",
    "            sum += sum_temp % 10\n",
    "            mul *= mul_temp % 10\n",
    "            n //= 10\n",
    "        return mul - sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        n = str(n)\n",
    "        a1 = 1\n",
    "        a2 = 0\n",
    "        for i in n:\n",
    "            a1 *= int(i)\n",
    "            a2 += int(i)\n",
    "        return a1 - a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        arrtempList = [int(j) for j in str(n)]\n",
    "        nTemp = arrtempList[0] \n",
    "        for i in range(1,len(arrtempList)):\n",
    "            nTemp = nTemp*arrtempList[i]\n",
    "        \n",
    "        return (nTemp-sum(arrtempList) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        def multiply(numbers):\n",
    "            result = 1\n",
    "            for num in numbers:\n",
    "                result *= num\n",
    "            return result\n",
    "        def sum1(numbers):\n",
    "            result = 0\n",
    "            for num in numbers:\n",
    "                result += num\n",
    "            return result\n",
    "        a = sum1(int(i) for i in str(n))\n",
    "        b = multiply(int(i) for i in str(n))\n",
    "        return b-a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "      \n",
    "      sum = 0\n",
    "      ji = 1\n",
    "      while n:\n",
    "        mid = n % 10\n",
    "        n //= 10\n",
    "        sum += mid\n",
    "        ji *= mid\n",
    "      return ji - sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        result = 0\n",
    "        num1 = n\n",
    "        num2 = n\n",
    "        num1_sum = 0\n",
    "        num2_x = 1\n",
    "        while num1:\n",
    "            num1_sum += num1 % 10\n",
    "            num1 //= 10\n",
    "        while num2:\n",
    "            num2_x *= num2 % 10\n",
    "            num2 //= 10\n",
    "        return num2_x - num1_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ji=1\n",
    "        he=0\n",
    "        for  i in str(n):\n",
    "            i=int(i)\n",
    "            ji*=i\n",
    "            he+=i\n",
    "        return ji-he"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        b=1\n",
    "\n",
    "        for i in range(len(str(n))):\n",
    "            b*=int((str(n)[i]))\n",
    "        return (b-sum([int(x) for x in str(n)]))    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        lst = list(map(int, str(n)))\n",
    "        return prod(lst) - sum(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        nums = list(map(int, str(n)))\n",
    "        return reduce(mul, nums) - sum(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s=0\n",
    "        mcl=1\n",
    "        while n!=0:\n",
    "            s+=n%10\n",
    "            mcl*=n%10\n",
    "            n=n//10\n",
    "        return mcl-s\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        int_pro = 1\n",
    "        int_sum = 0\n",
    "        for s in str(n):\n",
    "            item = int(s)\n",
    "            int_pro *= item\n",
    "            int_sum += item\n",
    "        return int_pro-int_sum\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        multi = 1\n",
    "        add = 0\n",
    "        while n != 0 :\n",
    "               add += n % 10\n",
    "               multi *= n % 10\n",
    "               n //= 10\n",
    "        return multi - add\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        acc = 1\n",
    "        total = 0\n",
    "        for i in s:\n",
    "            acc *= int(i)\n",
    "            total += int(i)\n",
    "        return acc - total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum1=0\n",
    "        sum2=1\n",
    "        while n:\n",
    "            sum1+=n%10\n",
    "            sum2*=n%10\n",
    "            n//=10\n",
    "        return sum2-sum1    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ans1 = 1\n",
    "        ans2 = 0\n",
    "        for i in str(n):\n",
    "            ans1 *= int(i)\n",
    "            ans2 += int(i)\n",
    "        return ans1 - ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        n_str=str(n)\n",
    "        for i in range(len(n_str)):\n",
    "            n=eval(\"+\".join(nums for nums in n_str))\n",
    "        x=n\n",
    "        for j in range(len(n_str)):\n",
    "            n=eval(\"*\".join(nums for nums in n_str))\n",
    "        y=n\n",
    "        return y-x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        return eval(\"*\".join(str(n))) - sum(int(x) for x in str(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s = 0\n",
    "        m = 1\n",
    "        while n:\n",
    "            x = n % 10\n",
    "            n = n // 10\n",
    "            s += x\n",
    "            m *= x\n",
    "        return m - s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        c = 1\n",
    "        s = [int(x) for x in str(n)]\n",
    "        j = sum(x for x in s)\n",
    "        for x in s:\n",
    "            c *= x\n",
    "        return c-j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a=1\n",
    "        b=0\n",
    "        while n :\n",
    "            temp = n%10\n",
    "            a*=temp\n",
    "            b+=temp\n",
    "            n = n//10\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m, s = 1, 0\n",
    "        while n != 0:\n",
    "            x, n = n % 10, n // 10\n",
    "            m *= x\n",
    "            s += x\n",
    "        return m - s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        he=0\n",
    "        ji=1\n",
    "        while n:\n",
    "            he+=n%10\n",
    "            ji*=n%10\n",
    "            n//=10\n",
    "        return ji-he\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m = str(n)\n",
    "        m_list = [int(i) for i in m]\n",
    "        y=1\n",
    "        for j in m_list:\n",
    "            y = j*y\n",
    "        h = sum(m_list)\n",
    "        return y-h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        digits = [int(c) for c in str(n)]\n",
    "        return reduce(operator.mul, digits) - sum(digits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum=0\n",
    "        product=1\n",
    "        while n>0:\n",
    "            m=n%10\n",
    "            sum+=m\n",
    "            product*=m\n",
    "            n//=10\n",
    "        return product-sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        return reduce(operator.mul, [int(char) for char in list(str(n))]) - sum([int(char) for char in list(str(n))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m = 1\n",
    "        a = 0\n",
    "        while n:\n",
    "            t = n % 10\n",
    "            m *= t\n",
    "            a += t\n",
    "            n //= 10\n",
    "        return m-a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "      n = str(n)\n",
    "      a = None\n",
    "      b = None\n",
    "      for i in n:\n",
    "        if a == None:\n",
    "          a = int(i)\n",
    "        else:\n",
    "          a = a*int(i)\n",
    "        if b == None:\n",
    "          b = int(i)\n",
    "        else:\n",
    "          b = b+int(i)\n",
    "      return a - b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        x = 1; y = 0\n",
    "        while n:\n",
    "            temp =  n % 10\n",
    "            x *= temp\n",
    "            y += temp\n",
    "            n //= 10\n",
    "        return x - y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        res_mul,res_sum=1,0\n",
    "        while n > 0:\n",
    "            tmp,n = n%10,n//10\n",
    "            res_mul*=tmp\n",
    "            res_sum+=tmp\n",
    "        return res_mul-res_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        iMul = 1\n",
    "        iSum = 0\n",
    "        while n:\n",
    "            remainder = n % 10\n",
    "            iMul *= remainder\n",
    "            iSum += remainder\n",
    "            n //= 10\n",
    "        \n",
    "        return iMul - iSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        mul, cnt = 1, 0\n",
    "        while n != 0:\n",
    "            mul *= n % 10\n",
    "            cnt += n % 10\n",
    "            n //= 10\n",
    "        return mul - cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        add = 0\n",
    "        product = 1\n",
    "        nums = self.fenchai(n)\n",
    "        for x in nums:\n",
    "            add += x\n",
    "            product *= x\n",
    "        return product - add\n",
    "\n",
    "\n",
    "    def fenchai(self, num:int) -> int:\n",
    "        ans = []\n",
    "        while num != 0:\n",
    "            ans.append(num % 10)\n",
    "            num = int(num / 10) \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m, s = 1, 0\n",
    "        while n != 0:\n",
    "            x, n = n % 10, n // 10\n",
    "            m *= x\n",
    "            s += x\n",
    "        return m - s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        n=[int(i) for i in list(str(n)) ]\n",
    "        num1=1\n",
    "        num2=0\n",
    "        for i in n:\n",
    "            num1*=i\n",
    "            num2+=i\n",
    "        return num1-num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        bitMul = 1\n",
    "        bitSum = 0\n",
    "        while n > 0:\n",
    "            cbit = n % 10\n",
    "            n //= 10\n",
    "            bitMul *= cbit\n",
    "            bitSum += cbit\n",
    "        return bitMul - bitSum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s,a = 0,1\n",
    "        while n != 0:\n",
    "             s += n % 10\n",
    "             a *= n % 10\n",
    "             n //= 10\n",
    "        return a - s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        stack=[]\n",
    "        while n>0:\n",
    "            stack.append(n%10)\n",
    "            n//=10\n",
    "        summary=sum(stack)\n",
    "        multiply=1\n",
    "        for i in stack:\n",
    "            multiply*=i\n",
    "        return multiply-summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        res, ans = 0, 1\n",
    "        while n > 0:\n",
    "            temp = n % 10\n",
    "            res += temp\n",
    "            ans *= temp\n",
    "            n //= 10\n",
    "        return ans - res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        digit_sum, digit_mul = 0, 1\n",
    "        for digit in str(n):\n",
    "            digit_sum += int(digit)\n",
    "            digit_mul *= int(digit)\n",
    "\n",
    "        return digit_mul - digit_sum\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        cc=1\n",
    "        su=0\n",
    "        while n:\n",
    "            n,vas=divmod(n,10)\n",
    "            cc*=vas\n",
    "            su+=vas\n",
    "        return cc-su\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum=0\n",
    "        ji=1\n",
    "        while n!=0:\n",
    "            temp=n%10\n",
    "            sum=sum+temp\n",
    "            ji=ji*temp\n",
    "            n=n//10\n",
    "\n",
    "        return ji-sum\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        res = 1\n",
    "        res1 = 0\n",
    "        while(n):\n",
    "            res *= n % 10\n",
    "            res1 += n % 10\n",
    "            n = n // 10\n",
    "        return res-res1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ad = 0\n",
    "        pro = 1\n",
    "        for i in str(n):\n",
    "            t = int(i)\n",
    "            ad+=t\n",
    "            pro*=t\n",
    "        return pro-ad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        ls = list(str(n))\n",
    "        m = 1\n",
    "        p = 0\n",
    "        for each in ls:\n",
    "            m *= int(each)\n",
    "            p += int(each)\n",
    "        return m-p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "      mul=1\n",
    "      sum=0\n",
    "      for i in str(n):\n",
    "        mul*=int(i)\n",
    "        sum+=int(i)\n",
    "      return mul-sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m = []\n",
    "        while n>9:\n",
    "            ind = n%10\n",
    "            m.append(ind)\n",
    "            n = int(n/10)\n",
    "        m.append(n)\n",
    "        y = 1\n",
    "        for j in m:\n",
    "            y = j*y\n",
    "        h = sum(m)\n",
    "        return y-h\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        m, s=1,0 \n",
    "        while n !=0:\n",
    "            x, n= n%10,n//10\n",
    "            m *=x\n",
    "            s +=x\n",
    "        return m-s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "         lst = list(map(int, str(n)))\n",
    "         return prod(lst) - sum(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        # 取数字的绝对值\n",
    "        num = abs(n)\n",
    "\n",
    "        # 初始化积和和\n",
    "        product = 1\n",
    "        digit_sum = 0\n",
    "\n",
    "        while num > 0:\n",
    "            digit = num % 10  # 取最后一位数字\n",
    "            product *= digit\n",
    "            digit_sum += digit\n",
    "            num //= 10  # 去掉最后一位数字\n",
    "\n",
    "        # 计算差值\n",
    "        difference = product - digit_sum\n",
    "\n",
    "        return difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        sum = 0\n",
    "        product = 1\n",
    "        for i in str(n):\n",
    "            sum += int(i)\n",
    "            product *= int(i)\n",
    "        return product - sum\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        s=0\n",
    "        m=1\n",
    "        for i in str(n):\n",
    "            s+=int(i)\n",
    "            m*=int(i)\n",
    "        return m-s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def subtractProductAndSum(self, n):\n",
    "        \"\"\"\n",
    "        :type n: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        product, s = 1, 0\n",
    " \n",
    "        while n:\n",
    "            n, digit = divmod(n, 10)\n",
    "            product *= digit \n",
    "            s += digit\n",
    "        return product - s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        mul=1\n",
    "        add=0\n",
    "        for i in range(0,len(str(n))):\n",
    "            mul=mul*int(str(n)[i])\n",
    "            add=add+int(str(n)[i])\n",
    "        return mul-add"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        a,b=1,0\n",
    "        while n:\n",
    "            t = n % 10\n",
    "            n = n // 10\n",
    "            a *= t\n",
    "            b += t\n",
    "        return a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def subtractProductAndSum(self, n: int) -> int:\n",
    "        temp1 = 1\n",
    "        temp2 = 0\n",
    "        while n > 0 :\n",
    "            yu = n % 10\n",
    "            temp1 *= yu\n",
    "            temp2 += yu\n",
    "            n //= 10\n",
    "        return temp1 - temp2"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
